const app = getApp()
const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    total: 0,
    startdate: (util.timestampToTime(Date.now() - 7 * 60 * 60 * 24 * 1000)),
    enddate: (util.timestampToTime(Date.now() - 0 * 60 * 60 * 24 * 1000)),
    navBarHeight: app.globalData.navBarHeight,
    bankList: [],
    bankidList: [],
    bankindex: '',
    branchList: [],
    branchidList: [],
    branchidindex: '',
    subBranchList: [],
    subBranchidList: [],
    subBranchindex: '',
    siteList: [],
    siteidList: [],
    siteindex: '',
    selectlist: [{name: '银行', num: 500, active: false}, {name: '分行', num: 500, active: false}, {name: '支行', num: 10000, active: false}, {name: '网点', num: 10000, active: false}],
    tablist: [],
    setTitleName: '活跃数',
    sortField: '',
    type: '',
    typeName: '',
    bannerInfo: '',
    queryForm: { bankId: '', bankBranchId: '', bankSubBranchId: '', bankWorkstationId: '', startDate: '', endDate: '', indexType: 'activityNum' },
    query: { bankName: '', bankBranchName: '', bankSubBranchName: '', bankWorkstationName: '' },
    params: { parentId: ''}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    wx.setNavigationBarTitle({
      title: options.typeName,
    })
    let queryForm = JSON.parse(options.queryForm)
    let queryIndex = JSON.parse(options.queryIndex)
    console.log(queryIndex)
    let fields = JSON.parse(options.fields)
    this.setData({
      queryForm: queryForm,
      startdate: queryForm.startDate,
      enddate: queryForm.endDate,
      fields: fields,
      type: options.type,
      typeName: options.typeName,
      bankindex: queryIndex.bankindex,
      branchidindex: queryIndex.branchidindex,
      subBranchindex: queryIndex.subBranchindex,
      siteindex: queryIndex.siteindex,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAllBankList()
    this.activeList()
    if (this.data.queryForm.bankId) {
      this.defaultBranch()
    }
    if (this.data.queryForm.bankBranchId) {
      this.defaultSubBranch()
    }
    if (this.data.queryForm.bankSubBranchId) {
      this.defaultSite()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  defaultBranch () {
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: {parentId: this.data.queryForm.bankId}
    }).then(res => {
      if (res.length > 0) {
        this.data.branchList = []
        this.data.branchidList = []
        res.map(item => {
            this.data.branchList.push(item.orgName)
            this.data.branchidList.push(item.id)
          })
      }
      this.setData({
        branchList: this.data.branchList,
        branchidList: this.data.branchidList,
      })
    })
  },
  defaultSubBranch () {
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: {parentId: this.data.queryForm.bankBranchId}
    }).then(res => {
      if (res.length > 0) {
        this.data.subBranchList = []
        this.data.subBranchidList = []
        res.map(item => {
            this.data.subBranchList.push(item.orgName)
            this.data.subBranchidList.push(item.id)
          })
      }
      this.setData({
        subBranchList: this.data.subBranchList,
        subBranchidList: this.data.subBranchidList
      })
    })
  },
  defaultSite () {
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: {parentId: this.data.queryForm.bankSubBranchId}
    }).then(res => {
      if (res.length > 0) {
        this.data.siteList = []
        this.data.siteidList = []
        res.map(item => {
            this.data.siteList.push(item.orgName)
            this.data.siteidList.push(item.id)
          })
      }
      this.setData({
        siteList: this.data.siteList,
        siteidList: this.data.siteidList
      })
    })
  },
  activeList () {
    const params = Object.assign({indexType: this.data.type}, this.data.queryForm)
    app.api.post({
      url: 'report_activity_activityBoardDetail',
      data: params
    }).then(res => {
      this.setData({
        bannerInfo: res.data,
        list: res.stuData ? res.stuData : []
      })
      console.log(this.data.bannerInfo, this.data.list)
    })
  },
  getcurrentOrgDetail () {
    app.api.get({
      url: 'cpic_org_getOrgLevel',
      data: { id: app.globalData.userInfo.orgId }
    }).then(res => {
      this.data.query.bankName = res.bank.id === app.globalData.userInfo.orgId ? '' : res.bank.orgName
      this.data.queryForm.bankId = res.bank ? res.bank.id : ''
      this.data.bankindex = res.bank.id === app.globalData.userInfo.orgId ? 0 : ''

      this.data.query.bankBranchName = (res.branch && res.branch.id) === app.globalData.userInfo.orgId ? '' : (res.branch && res.branch.orgName)
      this.data.queryForm.bankBranchId = res.branch ? res.branch.id : ''
      this.data.branchidindex = (res.branch && res.branch.id) === app.globalData.userInfo.orgId ? 0 : ''
      
      this.data.query.bankSubBranchName = (res.subBranch && res.subBranch.id) === app.globalData.userInfo.orgId ? '' : (res.subBranch && res.subBranch.orgName)
      this.data.queryForm.bankSubBranchId = res.subBranch ? res.subBranch.id : ''
      this.data.subBranchindex = (res.subBranch && res.subBranch.id) === app.globalData.userInfo.orgId ? 0 : ''

      this.data.query.bankWorkstationName = res.site ? '' : ''
      this.data.queryForm.bankWorkstationId = res.site ? res.site.id : ''
      this.data.siteindex = res.site ? 0 : ''
      this.setData({
        query: this.data.query,
        queryForm: this.data.queryForm,
        bankindex: this.data.bankindex,
        branchidindex: this.data.branchidindex,
        subBranchindex: this.data.subBranchindex,
        siteindex: this.data.siteindex
      })
    })
  },
  bankChange: function(e) {
    this.data.queryForm.bankId = this.data.bankidList[e.detail.value]
    this.data.params.parentId = this.data.bankidList[e.detail.value]
    this.setData({
      bankindex: e.detail.value,
      params: this.data.params
    })
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: this.data.params
    }).then(res => {
      if (res.length > 0) {
        this.data.branchList = []
        this.data.branchidList = []
        res.map(item => {
            this.data.branchList.push(item.orgName)
            this.data.branchidList.push(item.id)
          })
      } else {
        this.data.branchList = []
        this.data.subBranchList = []
        this.data.siteList = []
      }
      this.data.queryForm.bankBranchId = ''
      this.data.queryForm.bankSubBranchId = ''
      this.data.queryForm.bankWorkstationId = ''
      this.setData({
        branchList: this.data.branchList,
        subBranchList: this.data.subBranchList,
        siteList: this.data.siteList,
        branchidList: this.data.branchidList,
        branchidindex: '',
        subBranchindex: '',
        siteindex: '',
        queryForm: this.data.queryForm
      }, () => {
        console.log('赋值成功')
        this.activeList()
      })
    })
  },
  bankCancel: function () {
    this.data.queryForm.bankId = ''
    this.data.queryForm.bankBranchId = ''
    this.data.queryForm.bankSubBranchId = ''
    this.data.queryForm.bankWorkstationId = ''
    this.data.branchList = []
    this.data.subBranchList = []
    this.data.siteList = []
    this.setData({
      bankindex: '',
      branchidindex: '',
      subBranchindex: '',
      siteindex: '',
      queryForm: this.data.queryForm,
      branchList: this.data.branchList,
      subBranchList: this.data.subBranchList,
      siteList: this.data.siteList
    })
    this.activeList()
  },
  branchChange: function(e) {
    this.data.queryForm.bankBranchId = this.data.branchidList[e.detail.value]
    this.data.params.parentId = this.data.branchidList[e.detail.value]
    this.setData({
      branchidindex: e.detail.value,
      params: this.data.params
    })
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: this.data.params
    }).then(res => {
      if (res.length > 0) {
        this.data.subBranchList = []
        this.data.subBranchidList = []
        res.map(item => {
            this.data.subBranchList.push(item.orgName)
            this.data.subBranchidList.push(item.id)
          })
      } else {
        this.data.subBranchList = []
        this.data.siteList = []
      }
      this.data.queryForm.bankSubBranchId = ''
      this.data.queryForm.bankWorkstationId = ''
      this.setData({
        queryForm: this.data.queryForm,
        subBranchindex: '',
        siteindex: '',
        subBranchList: this.data.subBranchList,
        siteList: this.data.siteList,
        subBranchidList: this.data.subBranchidList
      }, () => {
        this.activeList()
      })
    })
  },
  branchCancel: function() {
    this.data.queryForm.bankBranchId = ''
    this.data.queryForm.bankSubBranchId = ''
    this.data.queryForm.bankWorkstationId = ''
    this.data.subBranchList = []
    this.data.siteList = []
    this.setData({
      branchidindex: '',
      subBranchindex: '',
      siteindex: '',
      queryForm: this.data.queryForm,
      subBranchList: this.data.subBranchList,
      siteList: this.data.siteList
    })
    this.activeList()
  },
  subBranchChange: function(e) {
    console.log(e, this.data.subBranchList)
    this.data.queryForm.bankSubBranchId = this.data.subBranchidList[e.detail.value]
    this.data.params.parentId = this.data.subBranchidList[e.detail.value]
    this.setData({
      subBranchindex: e.detail.value,
      params: this.data.params
    })
    app.api.post({
      url: 'userAccessableBank_getAccessableBankOrgChildrenByParentId',
      data: this.data.params
    }).then(res => {
      if (res.length > 0) {
        this.data.siteList = []
        this.data.siteidList = []
        res.map(item => {
            this.data.siteList.push(item.orgName)
            this.data.siteidList.push(item.id)
          })
      } else {
        this.data.siteList = []
      }
      this.data.queryForm.bankWorkstationId = ''
      this.setData({
        queryForm: this.data.queryForm,
        siteindex: '',
        siteList: this.data.siteList,
        siteidList: this.data.siteidList
      }, () => {
        this.activeList()
      })
    })
  },
  subBranchCancel: function() {
    this.data.queryForm.bankSubBranchId = ''
    this.data.queryForm.bankWorkstationId = ''
    this.data.siteList = []
    this.setData({
      subBranchindex: '',
      siteindex: '',
      queryForm: this.data.queryForm,
      siteList: this.data.siteList
    })
    this.activeList()
  },
  siteChange: function(e) {
    this.data.queryForm.bankWorkstationId = this.data.siteidList[e.detail.value]
    this.setData({
      siteindex: e.detail.value,
      queryForm: this.data.queryForm
    })
    this.activeList()
  },
  siteCancel: function() {
    this.data.queryForm.bankWorkstationId = ''
    this.setData({
      siteindex: '',
      queryForm: this.data.queryForm
    })
    this.activeList()
  },
  // 开始时间
  bindstartDateChange: function(e) {
    this.setData({
      startdate: e.detail.value
    })
    this.data.queryForm.startDate = e.detail.value
    this.setData({
      queryForm: this.data.queryForm
    })
    this.activeList()
  },
  // 结束时间
  bindendDateChange: function(e) {
    this.setData({
      enddate: e.detail.value
    })
    this.data.queryForm.endDate = e.detail.value
    this.setData({
      queryForm: this.data.queryForm
    })
    this.activeList()
  },
  // 获取所有银行
  getAllBankList () {
    app.api.post({
      url: 'userAccessableBank_getAccessableBank',
      data: {}
    }).then(res => {
      // 银行
      if (res.length > 0) {
        res.map(item => {
          this.data.bankList.push(item.orgName)
          this.data.bankidList.push(item.id)
        })
      }
      this.setData({
        bankList: this.data.bankList,
        bankidList: this.data.bankidList,
      })
    })
  }
})